package mysql

import "douyin/models"

// SelectVidIdsByTime 查询Vid的Ids
func SelectVidIdsByTime(latestTime int64, fNumber int) (vidIds []int64, err error) {
	//tx := db.Debug().Model(&models.Vid{}).Select("video_id").Where("create_time<=?", latestTime).Order("create_time DESC").Limit(fNumber).Find(&vidIds)
	//TODO
	tx := db.Debug().Model(&models.Vid{}).Select("video_id").Where("create_time<=?", latestTime).Order("RAND()").Limit(fNumber).Find(&vidIds)
	//找不到记录，报错
	if len(vidIds) == 0 {
		return nil, ErrorQueryZeroRecord
	}
	//未知错误
	if tx.Error != nil {
		return nil, tx.Error
	}
	return
}

// SelectTimeByVid 通过Vid的Id查询Vid的创建时间
func SelectTimeByVid(vidId int64) (nextTime int64, err error) {
	//只需要一个
	tx := db.Debug().Model(&models.Vid{}).Select("create_time").Where("video_id = ?", vidId).First(&nextTime)
	if nextTime == 0 {
		err = ErrorQueryZeroRecord
		return
	}
	if tx.Error != nil {
		err = ErrorQueryFailed
		return
	}
	return
}
